package com.fishery.order.controller;


import com.fishery.entity.Result;
import com.fishery.order.entity.Order;
import com.fishery.order.entity.vo.OrderVo;
import com.fishery.order.entity.vo.SearchOrderConditionVo;
import com.fishery.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author run 、ghost
 * @since 2021-01-31
 */
@Api(tags = "订单管理相关接口")
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderService orderService;

	@PostMapping
	@ApiOperation("创建订单")
	public Result createOrder(@RequestBody @ApiParam(required = true, value = "order") Order order) {
		return orderService.createOrder(order)
				? Result.success("创建成功") : Result.error("创建失败");
	}

	@PutMapping
	@ApiOperation("修改订单")
	public Result updateOrder(@RequestBody @ApiParam(required = true, value = "orderVo") OrderVo orderVo) {
		if (orderVo == null || StringUtils.isEmpty(orderVo.getId())) {
			return Result.error("修改失败");
		}
		return orderService.updateOrder(orderVo)
				? Result.success("修改成功") : Result.error("修改失败");
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除订单")
	public Result deleteOrder(@PathVariable(value = "id") String orderId) {
		if (StringUtils.isEmpty(orderId)) {
			return Result.error("删除失败");
		}
		return orderService.deleteOrder(orderId)
				? Result.success("删除成功") : Result.error("删除失败");
	}

	@PutMapping("/deliver/{id}")
	@ApiOperation("为订单执行发货操作")
	public Result deliverOrder(@PathVariable(value = "id") String orderId) {
		if (StringUtils.isEmpty(orderId)) {
			return Result.error("发货失败");
		}
		return orderService.deliverOrder(orderId)
				? Result.success("发货成功") : Result.error("发货失败");
	}

	@PutMapping("/receive/{id}")
	@ApiOperation("为指定物流订单的产品订单执行送达操作(物流模块调用)")
	public Result receiveOrder(@PathVariable(value = "id") String logisticsId) {
		if (StringUtils.isEmpty(logisticsId)) {
			return Result.success("操作失败");
		}
		return orderService.receiveOrder(logisticsId)
				? Result.success("操作成功") : Result.success("操作失败");
	}

	@GetMapping("/{id}")
	@ApiOperation("根据订单id获取订单信息")
	public Result searchOrderById(@PathVariable(value = "id") String orderId) {
		if (StringUtils.isEmpty(orderId)) {
			return Result.error("查询失败");
		}
		return Result.success()
				.data(orderService.searchOrderById(orderId));
	}

	@GetMapping("/baseOrder/{id}")
	@ApiOperation("获取基地的所有订单")
	public Result searchOrderOfBase(@PathVariable(value = "id") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		return Result.success()
				.data((orderService.searchOrderOfBase(baseId)));
	}

	@GetMapping("/baseOrder/{id}/{page}/{size}")
	@ApiOperation("分页获取基地的订单")
	public Result searchOrderOfBase(@PathVariable(value = "id") String baseId,
									@PathVariable(value = "page") Integer page,
									@PathVariable(value = "size") Integer size) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		return Result.success()
				.data(orderService.searchOrderOfBase(baseId, page, size));
	}

	@PostMapping("/baseOrder/sale")
	@ApiOperation("查询基地的售出订单（领导仓）")
	public Result searchSaleOrderOfBase(@RequestBody SearchOrderConditionVo searchOrderConditionVo) {
		if (searchOrderConditionVo == null
				|| StringUtils.isEmpty(searchOrderConditionVo.getBaseId())) {
			return Result.error("查询失败");
		}
		return Result.success().data(orderService.searchSaleOrderOfBase(searchOrderConditionVo));
	}

}

